#!/bin/sh

#parse arguments

jobid="$1"
filename="$2"

filenameNoEXT=`echo $filename | sed 's/\(.*\)\..*/\1/'`
extension=`echo ${filename} |awk -F . '{print $NF}'`
echo "Processing jobid: $jobid ($filename)"

filepath="jobs/$jobid/$filename"

#create temp directory
`mkdir -p /var/www/docdiscuss/jobs/$jobid`
#create thumbnail directories
`mkdir -p /var/www/docdiscuss/jobs/$jobid/thumbnails`
#create xml directory
`mkdir -p /var/www/docdiscuss/jobs/$jobid/xml`

pdfpath="/var/www/docdiscuss/jobs/$jobid/$filenameNoEXT.pdf"

#file conversion
if [ "${extension}" = "pdf" ]; then
	echo "PDF file uploaded, no conversion required"
else
	echo "Non-PDF file upload, converting using openoffice filters"
	output=`java -jar /var/www/docdiscuss/lib/converter.jar /var/www/docdiscuss/jobs/$jobid/$filename $pdfpath`
fi 

#xml generation from pdf

`/var/www/docdiscuss/script/pdftoxml -cutPages $pdfpath /var/www/docdiscuss/jobs/$jobid/xml/master.xml`

#thumbnail generation

#-r flag behind jpeg generates the different sizes

output="/var/www/docdiscuss/jobs/${jobid}/thumbnails/${filenameNoEXT}"

first=`convert ${pdfpath} ${output}.jpg`
